package com.example.domet.controller;

import com.example.domet.bean.Employ;
import com.example.domet.bean.fengzhuang;
import com.example.domet.service.DeptService;
import com.example.domet.service.EmployService;
import com.example.domet.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController  // == @Controller + @ResponseBody  整个类的所有方法都是ajax的响应
@RequestMapping("/employ")
public class EmployController {

    private EmployService employService;
   private DeptService deptService;
     @Autowired
    public void setDeptService(DeptService deptService) {
        this.deptService = deptService;
    }

    @Autowired
    public void setEmployService(EmployService employService) {
        this.employService = employService;
    }


    @RequestMapping("/emps")
    public Result listAllEmploy(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Employ> employs = employService.listAllEmploy();
        for (Employ a:employs) {
            a.setBumen(deptService.bumenchaxun(a.getDeptId()));
        }
        PageInfo<Employ> pageInfo = new PageInfo<>(employs);
        return Result.ok().data("pageInfo",pageInfo);
    }

    @PostMapping("/checkNameExists")
    public Result checkNameExists(@RequestParam("empName")String empName){
        //-- 判断
        String regex = "[\u2E80-\u9FFF]{2,5}";
        if (!empName.matches(regex)){
            return Result.error().message("员工姓名必须是中文,且长度是2~5位");
        }

        boolean result =employService.checkEmpNameExists(empName);
        if (result)
            return Result.ok();
        else
            return Result.error().message("姓名重复");
    }
}
